package com.yeskery.nut.web.demo.controller;

import com.yeskery.nut.annotation.bean.Autowired;
import com.yeskery.nut.annotation.web.*;
import com.yeskery.nut.core.InputStreamResource;
import com.yeskery.nut.core.MediaType;
import com.yeskery.nut.core.MultipartFile;
import com.yeskery.nut.http.BasicMultipartFile;
import com.yeskery.nut.util.IOUtils;
import com.yeskery.nut.web.demo.feign.HttpFeignClient;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 上传controller
 * @author YESKERY
 * 2024/8/15
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private HttpFeignClient httpFeignClient;

    @PostMapping("/file")
    public String uploadFileByFromData(@RequestParam("file") MultipartFile file) throws Exception {
        IOUtils.transferInputStream(file.getInputStream(), Files.newOutputStream(Paths.get("C:/Users/YESKERY/Downloads/" + file.getFileName())));
        return "success";
    }

    @PostMapping("/uploadTextByRequestBody")
    public String uploadTextByRequestBody(@RequestBody InputStreamResource resource) {
        return IOUtils.readAsString(resource.getInputStream());
    }

    @PostMapping("/uploadTextByFromData")
    public String uploadTextByFromData(@RequestParam("file") MultipartFile file) {
        return IOUtils.readAsString(file.getInputStream());
    }

    @PostMapping("/uploadTextByMultiFromData")
    public String uploadTextByMultiFromData(@RequestParam("file") MultipartFile file, @RequestParam("name") String name) {
        return name + IOUtils.readAsString(file.getInputStream());
    }

    @PostMapping("/uploadTextByMaxFromData")
    public String uploadTextByMaxFromData(@RequestParam("file1") MultipartFile file1, @RequestParam("name") String name,
                                          @RequestParam("file2") MultipartFile file2, @RequestParam("age") String age) {
        return name + IOUtils.readAsString(file1.getInputStream()) + age + IOUtils.readAsString(file2.getInputStream());
    }

    @PostMapping("/testUploadTextByRequestBody")
    public String testUploadTextByRequestBody() {
        return httpFeignClient.uploadTextByRequestBody(() -> new ByteArrayInputStream("hello word".getBytes(StandardCharsets.UTF_8)));
    }

    @PostMapping("/testUploadTextByFromData")
    public String testUploadTextByFromData() {
        return httpFeignClient.uploadTextByFromData(new BasicMultipartFile("test.txt", MediaType.TEXT_PLAIN.getValue(), "hello word".getBytes(StandardCharsets.UTF_8)));
    }

    @PostMapping("/testUploadTextByMultiFromData")
    public String testUploadTextByMultiFromData() {
        return httpFeignClient.uploadTextByMultiFromData(new BasicMultipartFile("test.txt", MediaType.TEXT_PLAIN.getValue(), "hello word".getBytes(StandardCharsets.UTF_8)), "tom");
    }

    @PostMapping("/testUploadTextByMaxFromData")
    public String testUploadTextByMaxFromData() {
        return httpFeignClient.uploadTextByMaxFromData(new BasicMultipartFile("test.txt", MediaType.TEXT_PLAIN.getValue(), "hello word".getBytes(StandardCharsets.UTF_8)), "tom",
                new BasicMultipartFile("test.txt", MediaType.TEXT_PLAIN.getValue(), "java".getBytes(StandardCharsets.UTF_8)), "18");
    }
}
